<template>
  <div class="login">
    <!-- 头部标题 -->
    <van-nav-bar>
      <template #left>
        <i class="iconfont icon_nav_back"></i>
      </template>
    </van-nav-bar>
    <h2>你好，请登录</h2>
    <!-- 手机号和验证码 -->
    <van-form @submit="onSubmit">
      <!-- @submit="onSubmit" -->
      <van-cell>
        <van-field
          :rules="rules.mobile"
          placeholder="请输入手机号"
          v-model="mobile"
        >
          <!-- v-model="user.mobile" -->
          <template #left-icon>
            <i class="iconfont icon-phone"></i>
          </template>
        </van-field>
        <van-field
          :rules="rules.code"
          placeholder="请输入验证码"
          v-model="code"
        >
          <template #left-icon>
            <i class="iconfont icon-Lock"></i>
          </template>
          <template #button>
            <van-button plain type="info" @click="btnCode"
              >获取验证码</van-button
            >
          </template>
        </van-field>
      </van-cell>
      <p class="deal">
        登录既同意<span>《用户使用协议》</span>和<span>《隐私协议》</span>
      </p>
      <!-- 登录按钮 -->
      <div class="loginbtn">
        <van-button
          type="danger"
          size="large"
          :loading="isLoading"
          loading-type="spinner"
          round
          @click="disLogin"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { getCode, login } from '@/api/api.js'
import { setLocal } from '@/utils/local.js'

export default {
  data () {
    return {
      // 用户
      mobile: '15016624915',
      code: '',
      rules: {
        code: [
          { required: true, trigger: 'onBlur' },
          { pattern: /^\d{4}$/, message: '请输入4位验证码', trigger: 'onBlur' }
        ],
        mobile: [
          { required: true, trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入11位手机号码',
            trigger: 'onBlur'
          }
        ]
      },
      // 设置登录按钮的加载状态
      isLoading: false
    }
  },
  methods: {
    // 获取验证码
    async btnCode () {
      const res = await getCode({ mobile: this.mobile })
      var data = res.data
      this.$toast.success(data)
    },
    // 登录
    async disLogin () {
      const res = await login({
        mobile: this.mobile,
        code: this.code
      })
      if (res.code === 200) {
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } else {
        this.$toast.fail('登录失败')
      }
      setLocal(res.data.jwt, 'token')
      // JSON.stringify(res.data.data)
      // JSON.stringify(res.data.jwt)
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  /deep/ .icon_nav_back {
    color: #000;
    font-size: 24px;
  }
  /deep/h2 {
    padding: 30px 0px 55px 15px;
  }
  .van-nav-bar__title {
    color: #fff;
  }
  .van-cell {
    // height: 60px;
    // line-height: 60px;
    font-size: 20px;
    padding: 7px 9px;
  }
  .van-button {
    border: #fff solid 0;
    font-size: 20px;
    // padding: 5px;
    border-left: #ccc solid 1px;
  }
  p {
    font-size: 12px;
    margin-left: 15px;
    span {
      color: #1989fa;
    }
  }
}
.loginbtn {
  padding: 20px;
}
</style>
